import { EggAppConfig, PowerPartial, Context } from 'egg';

// for config.{env}.ts
export type DefaultConfig = PowerPartial<EggAppConfig & BizConfig>;

// app special config scheme
export interface BizConfig {
  sourceUrl: string;
}

export default (appInfo: EggAppConfig) => {
  const config = {} as PowerPartial<EggAppConfig> & BizConfig;

  // app special config
  config.sourceUrl = `https://github.com/eggjs/examples/tree/master/${appInfo.name}`;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1526818475458_1712';

  // add your config here
  config.middleware = ['errorHandler', 'authHandler'];

  config.errorHandler = {
    match: (ctx: Context) => {
      return ctx.path.startsWith('/api');
    },
  };

  config.authHandler = {
    match: (ctx: Context) => {
      return !ctx.path.startsWith('/api/v1/login')
        && !ctx.path.startsWith('/api/v1/admin/login')
        && !ctx.path.startsWith('/api/v1/bind-phone')
        && !ctx.path.startsWith('/public');
    },
  };

  // mysql
  config.mysql = {
    client: {
      host: 'localhost',
      port: '3307',
      user: 'root',
      password: '123456',
      database: 'schedule',
    },
    app: true,
    agent: false,
  };

  config.jwt = {
    secret: 'skdjbfjabdfnjdwaf',
    enable: true,
    match: (ctx: Context) => {
      return !ctx.path.startsWith('/api/v1/login')
        && !ctx.path.startsWith('/api/v1/admin/login')
        && !ctx.path.startsWith('/api/v1/bind-phone')
        && !ctx.path.startsWith('/public');
    },
  };

  config.weixin = {
    secret: 'a4a504ab8d479cca3acfa25bc4788784',
    appId: 'wxf85c7dc3c6853b6e',
  };

  config.security = {
    csrf: {
      ignore: () => true,
    },
  };

  // sms
  config.sms = {
    accessKeyId: 'LTAIDiqPaHFnZLTD',
    secretAccessKey: 'JGLZnUialTyLodtw0SzZmW3R2J9qd2',
  };

  return config;
};
